$(function(){
    var $add_student=$(".add_student")/*确定添加学生按钮*/,
        $stu_name=$("#stu_name")/*学生姓名输入框*/,
        $stu_sex=$("#stu_sex")/*学生性别下拉框*/,
        $phone=$("#stu_phone")/*手机号*/,
        $stu_yzmx=$("#stu_yzm")/*验证码输入框*/,
        $stu_kt = $("#stu_kt"),//开通选项下拉框
        $stu_sendcpwd=$("#stu_sendcpwd")/*是否下发账号密码下拉框*/;


    //判断名字是中文
    var checkNameRight=function(name){
        var myReg = /^[\u4e00-\u9fa5]+$/;
        if (myReg.test(name)){
            return true;
        } else {
            return false;;
        }
    };
    var checkPhoneNum=function(phonenum){//检查手机号
        if(phonenum==""){
            return false;
        }
        if(phonenum && ! /^1[34578]\d{9}$/g.test(phonenum)){
            return false;
        }
        return true;

    };
    var checkISInOtherClass=function(phonenum){//检查是否在别的班级
           return false;//先返回false
    };
    //开通选择
    $stu_kt.change(function(){
        var yzm= $stu_kt.val()
        console.log(yzm)
        if(yzm !=="2"){
            $('.yzm').hide();
        }else{
            $('.yzm').show();
        }
    })
    var check=function(){
        var yzm= $stu_kt.val();
        var _name = $.trim($stu_name.val());
        var _phone = $.trim($phone.val());
        var _yzmx = $.trim($stu_yzmx.val())
        var error={
            haserror:false,
            errormsg:""
        };
        if(_name.length>5||_name.length<2){
            error={
                haserror:true,
                errormsg:"姓名长度为2-5个汉字"
            };
        }else if (!checkNameRight(_name)){
            error={
                haserror:true,
                errormsg:"姓名长度为2-5个汉字"
            };
        }
        else if(!checkPhoneNum(_phone)){
            console.log(_phone);
            error={
                haserror:true,
                errormsg:"请输入正确的河南移动手机号"
            };
        }else if(yzm =="2"){
            if(_yzmx.length!==6 || isNaN(_yzmx)){
                error={
                    haserror:true,
                    errormsg:"请输入6位数字验证码"
                };
            }
        }
        return error;

    };
    //获取url数据
    function GetRequest() {
        var url = location.search; //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {//去掉？
            var str = url.substr(1);
            strs = str.split("&");//去掉&
            for(var i = 0; i < strs.length; i ++) {//所有去掉=  变成对象
                theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }
    var Request = new Object();
    Request = GetRequest();
    var number = Request.unit_class_id;
    console.log(number)

    //确定
    $add_student.click(function(){
        var error=check();
        if(error.haserror)
        {
            layer.open({
                content:error.errormsg,
                title:"",
                time: 1,
                style: 'border:none; background-color:#222222; color:#fff;'
            });
        }
        else{
            var studentInfo={
                unit_class_id:number,
                user_name:$stu_name.val(),
                sex:$stu_sex.val(),//性别1男 2女
                phone_number:$phone.val(),
                status:$stu_kt.val(),//1.短信开通，2验证码开通，3暂不开通
                sncode:$stu_yzmx.val(),
                send_user:$stu_sendcpwd.val()//是否下发账号密码 0不下发 1 下发
            };
            console.log(studentInfo);
            layer.open({
                type: 2,
                title: false,
                shadeClose: false
            });
            $.post('/account/add_student/',studentInfo, function(d) {
                console.log(studentInfo)
                layer.closeAll();
                if (d.response === "ok") {
                    layer.open({
                        content:"添加成功",
                        title:"",
                        time: 3,
                        style: 'border:none; background-color:#222222; color:#fff;'
                    });
                location.reload();
                } else {
                    layer.open({
                        content:d.error,
                        title:"",
                        time: 2,
                        style: 'border:none; background-color:#222222; color:#fff;'
                    });
                }
            }, 'json')
        }
    });
 });